﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace System.ComponentModel.Composition.Hosting
{
    /// <summary>
    /// mimic of the MEF DirectoryCatalog to use compatible structur for CLR creation.
    /// </summary>
    public class DirectoryCatalog : IDisposable
    {
        private String Path;
        /// <summary>
        /// All files from directory.
        /// </summary>
        internal IEnumerable<System.Reflection.Assembly> Files;
        public String LastLoadingFile;
        public DirectoryCatalog(String path)
        {
            this.Path = path;
            this.Files = this.LoadAssemblies();
        }
        /// <summary>
        /// Try to load all assemblies.
        /// </summary>
        /// <returns></returns>
        private IEnumerable<System.Reflection.Assembly> LoadAssemblies()
        {
            foreach (String file in System.IO.Directory.GetFiles(this.Path, "*.dll"))
            {
                try
                {
                    String f = System.IO.Path.GetFullPath(file);
                    this.LastLoadingFile = f;
                    yield return System.Reflection.Assembly.LoadFile(f);
                }
                finally
                {
                }
            }
        }

        #region dispose
        /// <summary>
        /// Finalize the Object
        /// </summary>
        ~DirectoryCatalog()
        {
            this.Dispose(false);
        }
        private void Dispose(Boolean disposing)
        {
            if (disposing == true)
            {
            }
            this.Files = null;
            this.Path = null;
        }
        /// <summary>
        /// Clean Up the object
        /// </summary>
        public void Dispose()
        {
            this.Dispose(true);
        }
        #endregion
    }
}
